package com.dcjt.firehotcarpad.widget

import android.annotation.SuppressLint
import android.content.Context
import android.media.MediaPlayer
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dcjt.firecommand.entity.RecordData
import com.dcjt.firehotcarpad.R
import com.dcjt.firehotcarpad.manager.DataManager
import com.dcjt.firehotcarpad.manager.MediaManager

import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.enums.PopupAnimation
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
import kotlinx.android.synthetic.main._xpopup_multiple_file.view.*
import kotlinx.android.synthetic.main.item_file_preview.view.*
import java.io.File

@SuppressLint("ViewConstructor")
class MultipleFilePopupView(private val data:List<String>, private val isAudio:Boolean, context: Context, private val isCti:Boolean) :BottomPopupView(context){

    override fun getImplLayoutId() = R.layout._xpopup_multiple_file

    override fun initPopupContent() {
        super.initPopupContent()
        rvFiles.layoutManager = LinearLayoutManager(context)
        val adapter = object :BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_file_preview,data.toMutableList()){
            override fun convert(helper: BaseViewHolder, item: String) {
                if(isAudio.not()){
                    helper.itemView.fileName.visibility = View.GONE
                    Glide.with(context).setDefaultRequestOptions(
                        RequestOptions().frame(1000000) /**单位微秒*/.centerCrop()
                    )
                        .load(DataManager.getInstance().appConfigEntity.appServerUrl.recordUrl + item)
                        .into(helper.itemView.ivImage)


                }else{
                    helper.itemView.fileName.visibility = View.VISIBLE
                    Glide.with(context).load(R.mipmap.icon_audio).into(helper.itemView.ivImage)
                    helper.itemView.fileName.text = File(item).name?:"未命名文件"
                }
            }

        }
        adapter.setOnItemClickListener { _, _, position ->
            val baseUrl = if(isCti) DataManager.getInstance().appConfigEntity.appServerUrl.recordUrl else DataManager.getInstance().appConfigEntity.appServerUrl.logUrl
            if(isAudio){
                XPopup.Builder(context)
                    .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                    .asCustom(RecordPopupWindow(context, true).apply {
                        this.initData(
                            mutableListOf(
                                RecordData(
                                    File(adapter.getItem(position)).name?:"未命名文件",
                                    "",
                                    "",
                                    baseUrl + adapter.getItem(position)
                                )
                            ), 0
                        )
                    })
                    .show()
//                if (MediaManager.isPlaying()) {
//                    MediaManager.release()
//                    return@setOnItemClickListener
//                }
//                MediaManager.playSound(
//                    DataManager.getInstance().configJCJEntity.serverUrl.recordUrl+adapter.getItem(position)
////                        "http://182.150.35.222:8813/new_bigdisaster.wav"
//                ) { mediaPlayer: MediaPlayer? ->
//                }
            }else{
                JCVideoPlayerStandard.startFullscreen(
                    context,
                    JCVideoPlayerStandard::class.java, baseUrl+data[position], File(adapter.getItem(position)).name?:"未命名文件"
                )

                // 视频预览
//                FilePreViewActivity.startActivity(FilePreViewActivity
//                    .VideoDetailInfo( File(adapter.getItem(position)).name?:"未命名文件",baseUrl+data[position]),context)
            }

        }
        rvFiles.adapter = adapter
    }

}